import AppUtil from "./app";

class CommonUtil {

  static filterNull(params) {
    return Object.keys(params)
      .filter((key) => params[key] !== null && params[key] !== undefined)
      .reduce((acc, key) => ({...acc, [key]: params[key]}), {});
  }

  static dateFtt(date, fmt) {
    let o = {
      "M+": date.getMonth() + 1,                 //月份
      "d+": date.getDate(),                    //日
      "h+": date.getHours(),                   //小时
      "m+": date.getMinutes(),                 //分
      "s+": date.getSeconds(),                 //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (let k in o)
      if (new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }

  static jumpToSomeWhere(url) {
    if (AppUtil.isH5()) {
      let targetGun = document.createElement("a");
      targetGun.href = url;
      targetGun.click();
    } else {
      uni.$u.route({
        url: 'pages/webview',
        params: {
          urlLink: url
        }
      })
    }
  }
  static dateWeek(date) {
    let day = date.getDay();
    let names = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    return names[day];
  }

  static guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
      let r = Math.random() * 16 | 0,
        v = c === 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    });
  }
}

export default CommonUtil